<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.send_to.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.newsletter.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
	



	$template = new template();
	$template->title = "Send this page to a friend";
	$template->head_gallery();



// FUNCTION // Display home page
function display_home($form_values){


	
	
?>




<h2>Send this page to a friend</h2>




<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>



      <form name="send_form" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
	  

		<input type="hidden" name="action" value="validate" />
		<input type="hidden" name="tidy_url" value="<?= $form_values['tidy_url'] ?>" />
		
<fieldset>


<legend>Email details</legend>


        <table class="aqua">
          <tr>
            <th width="120"><label>Your name</label></th>
            <td><input name="name" type="text" class="input_enquiry"  value="<?= $form_values['name'] ?>" maxlength="100"></td>
          </tr>
          <tr > 
            <th><label>Your email</label></th>
            <td><input name="epost1" type="text" class="input_enquiry"  value="<?= $form_values['epost1'] ?>" maxlength="100"></td>
          </tr>

          <tr > 
            <th><label>Your message</label></th>
			<td><textarea style="width: 220px; height: 55px" name='comments' class="textarea_enquiry"><?
			
	if(!$form_values['comments']){
		$form_values['comments'] = "Hi, I thought you might like to see this page...";
	}
	
	echo $form_values['comments'];
	
			 ?></textarea></td>
          </tr>
          <tr > 
            <th><label>Send to email</label></th>
            <td><input name="epost2" type="text" class="input_enquiry"  value="<?= $form_values['epost2'] ?>" maxlength="100"></td>
          </tr>
		  <tr>
		<td></td>
        <td><input type="checkbox" value="1" checked="checked" name="newsletter" /> 
		Add me to the <?= SITE_NAME ?> mailing list</td>
		</tr>
        </table>
		
</fieldset>


	<div class="a_center">
		  <input type="image" src="/images/button_send.gif" value="submit" />
	</div>



</form>




<?

}
// END FUNCTION 



// FUNCTION // Display home page
function display_sent(){

?>


<h2>Send this page to a friend</h2>

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>
	
<p>Thanks <strong><?= $_POST['name'] ?></strong>, your email has been sent to <strong><?= $_POST['epost2'] ?></strong>.</p>
	
<p><a href="javascript:self.close()">Click here to close this window.</a></p>


<?

}
// END FUNCTION 





switch ($_REQUEST['action']) {
	default;

		$page_url = MAIN_SITE_ROOT.$_GET['url'];
		$send_to = new send_to();
		$unique_id = $send_to->insert_url($page_url);
		$tidy_url = MAIN_SITE_ROOT.$unique_id."/send_page.html";
		$form_values['tidy_url'] = $tidy_url;
		display_home($form_values);
		
	break;
	case "validate";
		$send_to = new send_to();
		if($errors = $send_to->validate_send($_POST)){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "Your form had the following errors";
			$my_message['list'] = $errors;
			$GLOBALS['note'] = $my_message;
			display_home($_POST);
		}else{
			// Newsletter signup
			$newsletter = new newsletter();
			$params['email'] = $_REQUEST['epost1'];
			$params['user_ip'] = $_SERVER['REMOTE_ADDR'];
			if(!empty($_POST['newsletter'])) $newsletter->insert_email($params);
			unset($params);
			
			// Send email
			$send_to->send_email($_POST);
			$my_message = array();
			$my_message['type'] = 'pos';
			$my_message['message'] = "Your email has been sent";
			$GLOBALS['note'] = $my_message;
			display_sent();
		}
	break;
}




$template->foot_gallery(); 
	
	
?>